package com.oa.co.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.read.builder.ExcelReaderSheetBuilder;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import com.github.pagehelper.PageInfo;
import com.oa.co.bo.StudentBo;
import com.oa.co.common.PageVo;
import com.oa.co.common.R;
import com.oa.co.listener.WebStudentListener;
import com.oa.co.pojo.UserOa;
import com.oa.co.service.UserOaService;
import com.oa.co.utils.OssUtil;
import com.oa.co.vo.SexBarVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;

@CrossOrigin("*")
@RestController
@RequestMapping("/ajax1")
public class UserOaController {

    @Autowired
    private UserOaService studentService;


    @RequestMapping("/studentList")
    public PageVo<UserOa> tableJson(Integer page, Integer limit, String sname) {

        PageVo<UserOa> pageVo = new PageVo<>();

        PageInfo<UserOa> pageData = studentService.getPageData(page, limit, sname);

        // 把分页查询结果 封装到 layui 的 列表组件支持的数据结构中
        pageVo.setData(pageData.getList());


        pageVo.setCode(0);

        pageVo.setCount(pageData.getTotal());

        pageVo.setMsg("学员分页数据");

        return pageVo;
    }


    //删除功能
    @GetMapping("/deleteStudent")
    public R deleteStudent(Integer id) {
        int result = studentService.deleteStudent(id);

        if (result == 1) {
            return R.ok().message("删除成功");
        }
        return R.error().message("删除失败");
    }


    // 编辑功能
    @RequestMapping("/editStudent")
    public R editStudent(UserOa userOa) {

        int result = studentService.editStudent(userOa);
        System.out.println(result);
        if (result == 1) {

            return R.ok();
        } else {
            return R.error();
        }
    }

    //添加功能
    @RequestMapping("/addStudent")
    public R addStudent(UserOa userOa){

        int result =  studentService.addStudent(userOa);

        if (result==1){
            return R.ok();
        }
        return R.error();
    }


    @ResponseBody
    @RequestMapping("/upload")
    public R upload(MultipartFile file, String username, HttpServletRequest request) throws IOException {
        System.out.println(username);
        System.out.println(file);
        String filename = file.getOriginalFilename();
        InputStream inputStream = file.getInputStream();


        String url = OssUtil.uploadFile(inputStream, filename);

        return R.ok().data("url",url);
    }


    // 柱状图展示数据
    @RequestMapping("/bar")
    public  R echartsBar(){
        List<SexBarVo> list = studentService.sexBarData();
        return R.ok().data("sexBar",list);
    }


    // 上传excel 文件  读取excel  文件中的数据  存到数据库中
    @Autowired
    private WebStudentListener webStudentListener;

    @RequestMapping("/uploadExcel")
    public R readExcel(MultipartFile file) throws IOException {

        ExcelReaderBuilder builder = EasyExcel.read(file.getInputStream(), StudentBo.class, webStudentListener);

        ExcelReaderSheetBuilder sheet = builder.sheet();

        sheet.doRead();
        return R.ok();
    }


    //导出数据到excel 文件中
    @RequestMapping("/export")
    public void exportExcel(HttpServletRequest request, HttpServletResponse response, Integer page, Integer limit, String sname) throws IOException {
        //下载 excel  模板文件
        //设置下载excel 的 响应头
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");


        //防止中文乱码
        String filename = URLEncoder.encode("学生信息导出", "utf-8");

        //告诉浏览器  你要下载文件
        response.setHeader("Content-Disposition","attachment;filename="+filename+".xlsx");

        //得到一个 指向浏览器的输出流
        ServletOutputStream outputStream = response.getOutputStream();

        ExcelWriterBuilder write = EasyExcel.write(outputStream,StudentBo.class);

        ExcelWriterSheetBuilder sheet = write.sheet();

        //准备列表数据
        PageInfo<UserOa> pageData = studentService.getPageData(page,limit,sname);

        sheet.doWrite(pageData.getList());
    }
}
